using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Platform.Data;
using Platform.Menu;
using MenuItem = Platform.Menu.MenuItem;

namespace Web.Controls
{
    public partial class TreeControl : UserControl
    {
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
                GetParentMenuName(Convert.ToInt32(Session["Platform_CurrentNodeId"]));
            }
            divtree.Visible = false;
        }

        #endregion

        #region Fields

        //SQLMenuHelper _SQLMenuHelper = new SQLMenuHelper();

        #endregion

        #region Properties

        public bool ControlEnable
        {
            get { return ImageButton1.Enabled; }
            set { ImageButton1.Enabled = value; }
        }

        /// <summary>
        ///     ��ǰѡ�е�MenuID
        /// </summary>
        private string ParentMenuID
        {
            get
            {
                var o = Session["Platform_ParentMenuID"];
                return o == null ? string.Empty : o.ToString();
            }
            set { Session["Platform_ParentMenuID"] = value; }
        }

        #endregion

        #region TreeDataBind

        /// <summary>
        ///     �������ݰ
        /// </summary>
        public override void DataBind()
        {
            crTreeViewAllMenus.Nodes.Clear();
            var collection = MenuHelper.GetAllMenus();
            var topNode = new TreeNode();
            topNode.Text = "�����˵�";
            topNode.Value = "0";
            topNode.Expanded = true;
            crTreeViewAllMenus.Nodes.Add(topNode);
            collection = collection.Select("BelongID", "0", TlistQueryType.Precision);
            if (collection == null || collection.Count == 0)
                return;
            foreach (var menu in collection)
            {
                var node = CreateNode(menu);
                topNode.ChildNodes.Add(node);
                CreateSubTree(menu, node);
            }
        }

        private TreeNode CreateNode(MenuItem menu)
        {
            var node = new TreeNode();
            node.Text = menu.MenuName;
            node.Value = menu.MenuID.ToString();
            node.Expanded = false;
            return node;
        }

        private void CreateSubTree(MenuItem menu, TreeNode node)
        {
            var collection = MenuHelper.GetSubMenus(menu.MenuID);
            if (collection != null && collection.Count > 0)
                foreach (var childMenu in collection)
                {
                    var childNode = CreateNode(childMenu);
                    //node.Nodes.Add(childNode);
                    node.ChildNodes.Add(childNode);
                    CreateSubTree(childMenu, childNode);
                }
        }

        #endregion

        #region Method

        /// <summary>
        ///     �ڵ�����¼�
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void crTreeViewAllMenus_SelectedNodeChanged(object sender, EventArgs e)
        {
            var nodeID = int.Parse(crTreeViewAllMenus.SelectedNode.Value);
            if (nodeID == 0)
            {
                txtFatherMenuName.Text = "���˵�";
                Session["Platform_ParentMenuID"] = "0";
            }
            else
            {
                var menu = MenuHelper.SelectMenu(nodeID);
                txtFatherMenuName.Text = menu.MenuName;
                Session["Platform_ParentMenuID"] = menu.MenuID.ToString();
            }
            divtree.Visible = false;
        }

        /// <summary>
        ///     ��ʾ�·�����
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            divtree.Visible = true;
        }

        /// <summary>
        ///     �õ���ǰ�˵�����˵�������
        /// </summary>
        /// <param name="menuID"></param>
        public void GetParentMenuName(int menuID)
        {
            var menuItem = MenuHelper.SelectMenu(menuID);

            if (menuItem != null)
            {
                var finalMenuItem = MenuHelper.SelectMenu(menuItem.BelongID);

                if (finalMenuItem != null)
                {
                    txtFatherMenuName.Text = finalMenuItem.MenuName;
                    Session["Platform_ParentMenuID"] = finalMenuItem.MenuID.ToString();
                }
                else
                {
                    txtFatherMenuName.Text = "���˵�";
                    Session["Platform_ParentMenuID"] = "0";
                }
            }
        }

        /// <summary>
        ///     ���ݰ�ť������
        /// </summary>
        /// <param name="flag">trueΪ��Ӹ��˵� falseΪ����Ӳ˵�</param>
        public void SetControl(bool flag, int menuID)
        {
            if (flag)
            {
                txtFatherMenuName.Text = "���˵�";
                Session["Platform_ParentMenuID"] = "0";
            }
            else
            {
                var menuItem = MenuHelper.SelectMenu(menuID);
                if (menuItem != null)
                {
                    txtFatherMenuName.Text = menuItem.MenuName;
                    Session["Platform_ParentMenuID"] = menuItem.MenuID.ToString();
                }
            }
        }

        #endregion
    }
}